<?php
namespace app\interfaces\controller;

use app\interfaces\model\GoodsType as GoodsTypeModel;
use app\interfaces\model\ShopGoods as ShopGoodsModel;
use think\Db;
use think\Request;
use think\Exception;
use think\Cache;

class HotSale
{

    /*
     * 用户端 - 热卖榜列表
     */
    public function hot_list()
    {

        // 搜索条件
        $condition['parenTid'] = ['eq', '0'];



        $model = new GoodsTypeModel();
        $result = $model->where($condition)
            ->field('id,title')->order(['sort' => 'desc'])->paginate(15);

        foreach ($result as &$value) {
            // 是否开启审核
            $condition['goods_str'] = ['like','%,'.$value['id'].'%'];
            if (Cache::get('web_commodity_audit') == 'Y') $condition['status'] = ['eq', '1'];
            $value['goods'] = Db::name('shopGoods')->where($condition)->field('id,thumb_img,price,rebate_percentage,goods_str,sale_number')->order('sale_number desc')->limit(3)->select();
        }



        return ['code' => 200, 'message' => '成功', 'data' => $result];


    }

    /*
     * 用户端 - 二级分类
     */
    public function type_list()
    {

        $result = GoodsTypeModel::where(['status' => '1', 'count' => ['lt', '3']])
            ->field('id,title,parenTid')
            ->order(['id' => 'asc'])
            ->select();

        return ['code' => 200, 'message' => '成功', 'data' => recursion_node($result)];


    }

    /*
     * 用户端 - 热卖榜 更多>>
     * @param  string $parenTid 唯一标示
     */
    public function goods_list()
    {

        // 获取参数
        $parenTid = Request::instance()->param('parenTid');

        if (empty($parenTid)) throw new Exception('not Empty');

        // 搜索条件
        $condition['state'] = ['eq', 1];
        $condition['goods_str'] = ['like', '%,' . $parenTid . ',%'];

        // 是否开启审核
        if (Cache::get('web_commodity_audit') == 'Y') $condition['status'] = ['eq', '1'];

        $result = ShopGoodsModel::where($condition)
            ->field('id,title,price,thumb_img,sale_number,rebate_percentage')
            ->order(['sale_number' => 'desc', 'create_time' => 'desc'])
            ->select();

        return ['code' => 200, 'message' => '成功', 'data' => $result];


    }
}
